#include "led.h"

// #define red_path "/sys/class/leds/red/brightness"
#define led3_path "/sys/class/pwm/pwmchip7/"

void led_init(void)
{

    /*********************** LED子系统 **************************/
    // int fd;
    // fd = open(red_path, O_WRONLY);
    // if( fd < 0 ){
    //     printf("led file open error!\n");
    //     exit(-1);
    // }
    // while(1){
    //     write(fd, "255", sizeof("255"));
    //     usleep(500*1000);
    //     write(fd, "0", sizeof("0"));
    //     usleep(500*1000);
    // }
    /************************************************************/

    /************************** PWM *****************************/
    int fd;
    fd = open(led3_path"export", O_WRONLY);
    if( fd < 0 ){
        printf("led export file open error!\n");
        close(fd);
        exit(-1);
    }
    write(fd, "0", 1);
    
    fd = open(led3_path"pwm0/period", O_WRONLY);
    if( fd < 0 ){
        printf("led fd_period file open error!\n");
        close(fd);
        exit(-1);
    }
    write(fd, "1000000", 7);

    fd = open(led3_path"pwm0/duty_cycle", O_WRONLY);
    if( fd < 0 ){
        printf("led duty_cycle file open error!\n");
        close(fd);
        exit(-1);
    }
    write(fd, "1000000", 7);

    fd = open(led3_path"pwm0/polarity", O_WRONLY);
    if( fd < 0 ){
        printf("led polarity file open error!\n");
        close(fd);
        exit(-1);
    }
    write(fd, "normal", 6);

    fd = open(led3_path"pwm0/enable", O_WRONLY);
    if( fd < 0 ){
        printf("led enable file open error!\n");
        close(fd);
        exit(-1);
    }
    write(fd, "1", 1);
    /************************************************************/

    while (1)
    {
        usleep(500*1000);
        write(fd, "0", 1);
        usleep(500*1000);
        write(fd, "1", 1);
    }
    
    /************************************************************/

    close(fd);
}
